// pages/edit/edit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phone: '',
    address: '',
    where: '',
    myArr: [],
    myData: [],
    show: false,
    id: null,
    newlists: []
  },
  name(e) {//收货人
    this.setData({
      name: e.detail.value
    })
  },
  phone(e) {//电话
    this.setData({
      phone: e.detail.value
    })
  },
  address(e) {//所在地区
    this.setData({
      address: e.detail.value
    })
  },
  where(e) {//详细地址
    this.setData({
      where: e.detail.value
    })
    // if(this.data.where){
    //   wx.setStorageSync('where', this.data.where)
    // }
  },
  show(e) {//默认地址
    this.data.show = !this.data.show;
    e.target.dataset = this.data.show;
    // console.log(e.target.dataset);
    this.setData({
      show: e.target.dataset
    })
  },
  //跳转
  go() {
    if (this.data.id == null) {
      let num = this.data.myData.length;
      this.data.myArr = this.data.myData;
      if (this.data.show) {
        let tlists = this.data.myData;
        tlists.forEach(item => {
          item.show = false;
        })
        this.data.myArr.push({
          'name': this.data.name,
          'phone': this.data.phone,
          'address': this.data.address,
          'where': this.data.where,
          'show': this.data.show,
          'id': num
        })
        wx.setStorageSync('myArr', this.data.myArr)
        // console.log(tlists);
      } else {
        this.data.myArr.push({
          'name': this.data.name,
          'phone': this.data.phone,
          'address': this.data.address,
          'where': this.data.where,
          'show': this.data.show,
          'id': num
        })
        wx.setStorageSync('myArr', this.data.myArr)
      }
    } else {
      let index = this.data.newlists.findIndex(item => item.id == this.data.id)
      this.data.newlists[index].name = this.data.name,
        this.data.newlists[index].phone = this.data.phone,
        this.data.newlists[index].address = this.data.address,
        this.data.newlists[index].where = this.data.where
      if (this.data.show) {
        this.data.newlists.forEach(item => {
          item.show = false;
        })
        this.data.newlists[index].show = this.data.show
      }
      this.setData(this.data)
      wx.setStorageSync('myArr', this.data.newlists)
    }
    wx.switchTab({
      url: 'pages/address/address',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    if (options.id) {
      this.data.id = options.id;
    }
    this.setData({
      myData: wx.getStorageSync('myArr'),
      id: this.data.id
    })
    // console.log(this.data.myData);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.id) {
      let temp = wx.getStorageSync('myArr');
      this.data.newlists = temp
      // console.log(temp);
      if (temp) {
        let index = temp.findIndex(item => item.id == this.data.id)
        // console.log(temp[index]);
        this.data.name = temp[index].name,
          this.data.phone = temp[index].phone,
          this.data.address = temp[index].address,
          this.data.where = temp[index].where,
          this.data.show = temp[index].show
      }
      this.setData(this.data)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})